import React, { Component } from 'react';
import { Form, Row, Col } from 'antd';
import ShowType from '../../../routes/common/showType';
import styles from '../../../index.less';

const FormItem = Form.Item;

class TabProductionComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sTabId: props.app.currentPane.key, /* 当前标签 */
      componentName: props.componentName, /* 组件名 */
      masterConfig: props.masterConfig, /* 配置 */
      sId: props.sId, /* 业务数据Id */
      dataList: props.dataSet, /* 数据 */
      sActiveId: props.sActiveId, /* 窗体Id */
      disabled: props.disabled, /* 是否是查看状态 */
      sDispatchType: '', /* 生产派工方式 */
      sPieceWageFormulaId: '', /* 计件工资ID */
      sOutSideStyleType: '', /* 工单对应发外规格 */
      dMaxMachineLength: '', /* 上机最大长度 */
      dMaxMachineWidth: '', /* 上机最大宽度 */
      sProductionReportId: '', /* 产量上报方式 */
      sArrangeFormulaId: '', /* 工单折算产量公式 */
      sPieceWageFactFormulaId: '', /* 实际计件工资 */
      iMachine: '', /* 上机开数 */
      sReportParam: '', /* 上报参数设置 */
      sProductionMemo: '', /* 工艺要求 */
      sArrangeType: '', /* 排单分拆 */
      sCapacityFormulaId: '', /* 工单产能公式 */
    };
  }

  componentWillMount() {
    /* 获取主表数据 */
    const { masterConfig, sId } = this.state;
    if (masterConfig.gdsconfigformslave !== undefined) {
      const mainConfig = masterConfig.gdsconfigformslave;
      this.setState({
        sId: sId !== undefined ? sId : '',
        sDispatchType: mainConfig.filter(item => (item.sName.toLowerCase() === 'sdispatchtype'))[0],
        sPieceWageFormulaId: mainConfig.filter(item => (item.sName.toLowerCase() === 'spiecewageformulaid'))[0],
        sOutSideStyleType: mainConfig.filter(item => (item.sName.toLowerCase() === 'soutsidestyletype'))[0],
        dMaxMachineLength: mainConfig.filter(item => (item.sName.toLowerCase() === 'dmaxmachinelength'))[0],
        dMaxMachineWidth: mainConfig.filter(item => (item.sName.toLowerCase() === 'dmaxmachinewidth'))[0],
        sProductionReportId: mainConfig.filter(item => (item.sName.toLowerCase() === 'sproductionreportid'))[0],
        sArrangeFormulaId: mainConfig.filter(item => (item.sName.toLowerCase() === 'sarrangeformulaid'))[0],
        sPieceWageFactFormulaId: mainConfig.filter(item => (item.sName.toLowerCase() === 'spiecewagefactformulaid'))[0],
        iMachine: mainConfig.filter(item => (item.sName.toLowerCase() === 'imachine'))[0],
        sReportParam: mainConfig.filter(item => (item.sName.toLowerCase() === 'sreportparam'))[0],
        sProductionMemo: mainConfig.filter(item => (item.sName.toLowerCase() === 'sproductionmemo'))[0],
        sArrangeType: mainConfig.filter(item => (item.sName.toLowerCase() === 'sarrangetype'))[0],
        sCapacityFormulaId: mainConfig.filter(item => (item.sName.toLowerCase() === 'scapacityformulaid'))[0],
      });
    }
  }

  componentWillReceiveProps(nextProps) {
    const { disabled, dataSet } = nextProps;
    this.setState({ disabled, dataList: dataSet });
  }

  shouldComponentUpdate(nextProps, nextState) {
    const { componentName } = nextProps;
    const { sId } = this.props;
    if (sId === undefined) {
      return true;
    }
    let flag = true;
    if (nextState.disabled !== this.state.disabled) {
      return true;
    }
    if (this.state.sTabId === nextProps.app.currentPane.key && this.state.componentName === componentName) {
      flag = true;
    } else {
      flag = false;
    }
    return flag;
  }

  render() {
    const {
      app, form, handleModalPane, dataName, dataType, onClose, handleSelectChange,
    } = this.props;
    const {
      dataList, sId, sActiveId, disabled,
    } = this.state;
    const childProps = {
      app, form, sActiveId, dataList, sId, disabled, handleModalPane, dataName, dataType, onClose, handleSelectChange,
    };
    const {
      sDispatchType, sPieceWageFormulaId, sOutSideStyleType, dMaxMachineLength,
    } = this.state;
    const {
      dMaxMachineWidth, sProductionReportId, sArrangeFormulaId, sPieceWageFactFormulaId,
    } = this.state;
    const {
      iMachine, sReportParam, sProductionMemo, sArrangeType, sCapacityFormulaId,
    } = this.state;
    const sDispatchTypeProps = { showConfig: sDispatchType, ...childProps };
    const sPieceWageFormulaIdProps = { showConfig: sPieceWageFormulaId, ...childProps };
    const sOutSideStyleTypeProps = { showConfig: sOutSideStyleType, ...childProps };
    const dMaxMachineLengthProps = { showConfig: dMaxMachineLength, ...childProps };
    const dMaxMachineWidthProps = { showConfig: dMaxMachineWidth, ...childProps };
    const sProductionReportIdProps = { showConfig: sProductionReportId, ...childProps };
    const sArrangeFormulaIdProps = { showConfig: sArrangeFormulaId, ...childProps };
    const sPieceWageFactFormulaIdProps = { showConfig: sPieceWageFactFormulaId, ...childProps };
    const iMachineProps = { showConfig: iMachine, ...childProps };
    const sReportParamProps = { showConfig: sReportParam, ...childProps };
    const sProductionMemoProps = { showConfig: sProductionMemo, ...childProps };
    const sArrangeTypeProps = { showConfig: sArrangeType, ...childProps };
    const sCapacityFormulaIdProps = { showConfig: sCapacityFormulaId, ...childProps };
    return (
      <FormItem className={styles.searchSlaveForm}>
        <Row type="flex">
          <Col
            span={6}
            order={sDispatchType.iOrder > 24 ? 24 : sDispatchType.iOrder}
            style={{ display: sDispatchType.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sDispatchType" showDefaultName="生产派工方式(*)" {...sDispatchTypeProps} />
          </Col>
          <Col
            span={6}
            order={sPieceWageFormulaId.iOrder > 24 ? 24 : sPieceWageFormulaId.iOrder}
            style={{ display: sPieceWageFormulaId.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sPieceWageFormulaId" showDefaultName="计件工资ID" {...sPieceWageFormulaIdProps} />
          </Col>
          <Col
            span={6}
            order={sPieceWageFactFormulaId.iOrder > 24 ? 24 : sPieceWageFactFormulaId.iOrder}
            style={{ display: sPieceWageFactFormulaId.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sPieceWageFactFormulaId" showDefaultName="实际计件工资" {...sPieceWageFactFormulaIdProps} />
          </Col>
          <Col
            span={6}
            order={sReportParam.iOrder > 24 ? 24 : sReportParam.iOrder}
            style={{ display: sReportParam.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sReportParam" textArea showDefaultName="上报参数设置" {...sReportParamProps} />
          </Col>
          <Col
            span={6}
            order={dMaxMachineLength.iOrder > 24 ? 24 : dMaxMachineLength.iOrder}
            style={{ display: dMaxMachineLength.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dMaxMachineLength" showDefaultName="上机最大长度" {...dMaxMachineLengthProps} />
          </Col>
          <Col
            span={6}
            order={dMaxMachineWidth.iOrder > 24 ? 24 : dMaxMachineWidth.iOrder}
            style={{ display: dMaxMachineWidth.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dMaxMachineWidth" showDefaultName="上机最大宽度" {...dMaxMachineWidthProps} />
          </Col>
          <Col
            span={6}
            order={sProductionReportId.iOrder > 24 ? 24 : sProductionReportId.iOrder}
            style={{ display: sProductionReportId.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sProductionReportId" showDefaultName="产量上报方式(*)" {...sProductionReportIdProps} />
          </Col>
          <Col
            span={6}
            order={sProductionMemo.iOrder > 24 ? 24 : sProductionMemo.iOrder}
            style={{ display: sProductionMemo.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sProductionMemo" textArea showDefaultName="工艺要求" {...sProductionMemoProps} />
          </Col>
          <Col
            span={6}
            order={sArrangeType.iOrder > 24 ? 24 : sArrangeType.iOrder}
            style={{ display: sArrangeType.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sArrangeType" showDefaultName="排单分拆" {...sArrangeTypeProps} />
          </Col>
          <Col
            span={6}
            order={sCapacityFormulaId.iOrder > 24 ? 24 : sCapacityFormulaId.iOrder}
            style={{ display: sCapacityFormulaId.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sCapacityFormulaId" showDefaultName="工单产能公式" {...sCapacityFormulaIdProps} />
          </Col>
          <Col
            span={6}
            order={sOutSideStyleType.iOrder > 24 ? 24 : sOutSideStyleType.iOrder}
            style={{ display: sOutSideStyleType.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sOutSideStyleType" showDefaultName="工单发外对应规格" {...sOutSideStyleTypeProps} />
          </Col>
          <Col
            span={6}
            order={sArrangeFormulaId.iOrder > 24 ? 24 : sArrangeFormulaId.iOrder}
            style={{ display: sArrangeFormulaId.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sArrangeFormulaId" showDefaultName="工单折算产量公式" {...sArrangeFormulaIdProps} />
          </Col>
          <Col
            span={6}
            order={iMachine.iOrder > 24 ? 24 : iMachine.iOrder}
            style={{ display: iMachine.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="iMachine" showDefaultName="上机开数" {...iMachineProps} />
          </Col>
        </Row>
      </FormItem>
    );
  }
}

export default TabProductionComponent;
